package com.neuedu.test;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.neuedu.entity.BaseEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class AutoCoding {
    public static void main(String[] args) {
        // 定义url
        String url = "jdbc:mysql://192.168.80.131:3307/db?serverTimezone=Asia/Shanghai";
        // 定义项目路径
        String base_path = System.getProperty("user.dir");
        // 定义要生成的表名
        String table = "customernurseitem";
        // 定义代码生成路径
        Map<OutputFile, String> pathInfo = new HashMap<>();
        pathInfo.put(OutputFile.entity, base_path + "/src/main/java/com/neuedu/entity");
        pathInfo.put(OutputFile.mapper, base_path + "/src/main/java/com/neuedu/mapper");
        pathInfo.put(OutputFile.xml, base_path + "/src/main/resources/com/neuedu/mapper");
        pathInfo.put(OutputFile.service, base_path + "/src/main/java/com/neuedu/service");
        pathInfo.put(OutputFile.serviceImpl, base_path + "/src/main/java/com/neuedu/service/impl");
        pathInfo.put(OutputFile.controller, base_path + "/src/main/java/com/neuedu/controller");
        // 定义生成器
        FastAutoGenerator generator = FastAutoGenerator.create(url, "root", "123456");
        // 全局配置
        generator.globalConfig(builder -> {
            // 配置作者
            builder.author("wangyu");
            // 配置禁止打开目录
            builder.disableOpenDir();
        });
        // 包配置
        generator.packageConfig(builder -> {
            // 配置父包
            builder.parent("com.neuedu");
            // 配置代码生成路径
            builder.pathInfo(pathInfo);
        });
        // 策略配置
        generator.strategyConfig(builder -> {
            // 生成的表
            builder.addInclude(table);
            // entity策略
            builder.entityBuilder()
                    .naming(NamingStrategy.underline_to_camel)
                    .columnNaming(NamingStrategy.underline_to_camel)
                    .enableLombok()
                    .superClass(BaseEntity.class);
            // controller策略
            builder.controllerBuilder()
                    // restController
                    .enableRestStyle();
            // service配置
            builder.serviceBuilder()
                    // 去掉接口文件名开头的I
                    .formatServiceFileName("%sService");
        });
        // 模版配置
        generator.templateEngine(new FreemarkerTemplateEngine());
        // 生成代码
        generator.execute();
    }
}